package utils;

import Message.AccountSendMessageInfo;

import java.io.*;
import java.sql.Timestamp;


/**
 * 该类用来封装对聊天记录的存储和读写
 * */
public class ReadWriteFileByAccountSendMessageInfoFromOneList {
    private String LogPath;

    /**
     * 一旦初始化该类，类中的属性LogPath就会设为项目下的/Log文件夹下
     * */
    public ReadWriteFileByAccountSendMessageInfoFromOneList(){
        File directory = new File("");//设定为当前文件夹
        String LogPath = "";
        try{
            LogPath = directory.getCanonicalPath() + "/";//获取标准的路径
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.LogPath = LogPath + "Log/";
    }

    public String getLogPath() {
        return LogPath;
    }

    /**
     * 传入需要传入文件名和写入的内容，就可以把内容写入到/Log/<文件名> 中
     * */
    public  void writeInFile(String fileName, String content) {
        String filePathtemp = this.getLogPath() + fileName;
        File file = new File(filePathtemp);
        if (!file.exists()) {
            try {
                file.createNewFile(); //如果文件不存在则创建文件
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Writer writer = null;
        StringBuilder outputString = new StringBuilder();
        try {
            outputString.append(content + "\r\n");
            writer = new FileWriter(file, true); // true表示追加
            writer.write(outputString.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
    }

    /**
     * 该方法从文件中读取该用户与某一用户的聊天记录，AccountSendMessageInfoFromOneList.get(i)中的message.userIdInDatabaseFrom默认为该用户id，
     * userIdInDatabaseTo则默认为该用户的聊天对象的用户ID，参数userid为该用户的Uid
     * */
    public AccountSendMessageInfoFromOneList readFile(String filePath, String userid) throws IOException {
        String filePathtemp = this.getLogPath() + filePath;
        File file = new File(filePathtemp);
        InputStreamReader read = null;// 考虑到编码格式
        try {
            read = new InputStreamReader(new FileInputStream(file), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedReader bufferedReader = new BufferedReader(read);
        String lineTxt = null;
        AccountSendMessageInfoFromOneList accountSendMessageInfoFromOneList = new AccountSendMessageInfoFromOneList();
        while ((lineTxt = bufferedReader.readLine()) != null) {
            String[] splitstr=lineTxt.split("\t");
            String userIdInDatabaseFrom = splitstr[0];
            String userIdInDatabaseTo = splitstr[1];
            Timestamp time = Timestamp.valueOf(splitstr[2]);
            String messageInfo = splitstr[3];
            AccountSendMessageInfo accountSendMessageInfo = new AccountSendMessageInfo();
            if(userid.equals(userIdInDatabaseFrom)){
                accountSendMessageInfo.setUserIdInDatabaseFrom(userIdInDatabaseFrom);
                accountSendMessageInfo.setUserIdInDatabaseTo(userIdInDatabaseTo);
            }else{
                accountSendMessageInfo.setUserIdInDatabaseFrom(userIdInDatabaseTo);
                accountSendMessageInfo.setUserIdInDatabaseTo(userIdInDatabaseFrom);
            }
            accountSendMessageInfo.setTime(time);
            accountSendMessageInfo.setMessageInfo(messageInfo);
            accountSendMessageInfoFromOneList.add(accountSendMessageInfo);
        }
        read.close();
        return accountSendMessageInfoFromOneList;
    }

    public static void main(String[] args) throws IOException {
        ReadWriteFileByAccountSendMessageInfoFromOneList readWriteFile = new ReadWriteFileByAccountSendMessageInfoFromOneList();
        System.out.println(readWriteFile.getLogPath());
        String filePath = "0_888888889_888888881.txt";
        AccountSendMessageInfoFromOneList accountSendMessageInfoFromOneList = readWriteFile.readFile(filePath, "888888889");
//        System.out.println(JaxbUtil.convertToXml(accountSendMessageInfoFromOneList));
    }
}
